import { message } from 'ant-design-vue'
import { selectedRowKeys } from '../hooks/useTable'
import { requestApi, getTableData, emptySelectedRowKeys } from '../hooks/useTable'
import apis from '@/apis'
import { reactive, ref } from 'vue'
/**
 * 采集资源列表进资源库参数
 * @param id
 * @param resource_type
 * @param content_type
 * @param status
 * @param site_id
 * @returns
 */
interface CollectParams {
  id: string | undefined
  resource_type: number | undefined
  content_type: number | undefined
  status: number | undefined
  site_id: string | undefined
}
export const collectSourceParams = reactive<CollectParams>({
  id: undefined,
  resource_type: undefined,
  content_type: undefined,
  status: undefined,
  site_id: undefined
})
export const setCollectSourceStatus = (status: number) => {
  collectSourceParams.status = status
}
const getId = (id?: string) => {
  console.log('id: ', id)
  return id ? id : selectedRowKeys.value
}
//modal禁用弹窗
export const modalVIsible = ref(false)
//timeTask 禁用多个
export const modalTaskVisible = ref(false)
//modal删除弹窗
export const deleteVisible = ref(false)
//timeTask 删除多个
export const deleteTaskVisible = ref(false)
//停止采集状态
export const collectVisible = ref(false)
const resetFunc = () => {
  modalVIsible.value = false
  modalTaskVisible.value = false
  deleteVisible.value = false
  deleteTaskVisible.value = false
  collectVisible.value = false
}
//停止采集
const stopCollectRequestApi = ref()
//修改停止采集api
const setStopCollectRequestApi = (api: any) => {
  stopCollectRequestApi.value = api
}
// 停止采集
const stopCollectFunc = (id?: string): Promise<void> => {
  return new Promise((resolve, reject) => {
    const requestApi = stopCollectRequestApi.value
    const params = {
      id: getId(id)
    }
    requestApi(params)
      .then(() => {
        resolve()
        getTableData()
        emptySelectedRowKeys()
        message.success(`已停止`)
        resetFunc()
      })
      .catch(() => {
        reject()
      })
  })
}
//禁用启用状态
const startOrDisableRequestApi = ref()
//修改禁用启用状态api
const setStartOrDisableRequestApi = (api: any) => {
  startOrDisableRequestApi.value = api
}
//删除api
const deleteRequestApi = ref()
//修改禁用启用状态api
const setDeleteRequestApi = (api: any) => {
  deleteRequestApi.value = api
}
//启用/禁用
export type Status = 1 | 2 // 1启用 | 2禁用
const formatCollectStatus = (status: Status) => {
  switch (status) {
    case 1:
      return 2
    case 2:
      return 1
  }
}
const startOrDisableStatus = (status: Status, id?: string, modalMsg?: string) => {
  const _status = formatCollectStatus(status)
  if (_status === 1) {
    startOrDisableStatusHandle(status, id, modalMsg)
  } else if (_status === 2) {
    return startOrDisableStatusHandle(status, id, modalMsg)
  }
}
const getUpdateCollectMessage = (status: Status, id?: string) => {
  switch (formatCollectStatus(status)) {
    case 1:
      return Array.isArray(getId(id)) ? '批量启用' : '启用'
    case 2:
      return Array.isArray(getId(id)) ? '批量禁用' : '禁用'
  }
}
const startOrDisableStatusHandle = (status: Status, id?: string, modalMsg?: string): Promise<void> => {
  console.log('status: ', status)
  return new Promise<void>((resolve, reject) => {
    // const requestApi = Array.isArray(getId(id)) ? updateCollectStatusMultiple : updateCollectStatusSingle
    const requestApi = startOrDisableRequestApi.value
    const params: any = Array.isArray(getId(id))
      ? { batch_id: getId(id), status: formatCollectStatus(status) }
      : { id: getId(id), status: formatCollectStatus(status) }

    const messageStr = getUpdateCollectMessage(status, id)

    console.log('request：', requestApi, params, messageStr)

    requestApi(params)
      .then(() => {
        resolve()
        getTableData()
        emptySelectedRowKeys()
        message.success(`已${messageStr}${modalMsg}`)
        setCollectSourceStatus(status)
        resetFunc()
      })
      .catch(() => {
        reject()
      })
  })
}

// 删除
const { deleteCollectSingle, deleteCollectMultiple } = apis.collectManage
const getDeleteCollectMessage = (id?: string) => {
  return Array.isArray(getId(id)) ? '批量删除' : '删除'
}
const deleteResourceSite = (id?: string, modalMsg?: string) => {
  // const requestApi = Array.isArray(getId(id)) ? deleteCollectMultiple : deleteCollectSingle
  const requestApi = deleteRequestApi.value
  const params: any = Array.isArray(getId(id)) ? { ids: getId(id) } : { id: getId(id) }
  const messageStr = getDeleteCollectMessage(id)
  console.log('request：', requestApi, params, messageStr)
  requestApi(params).then(() => {
    getTableData()
    emptySelectedRowKeys()
    message.success(`已${messageStr}${modalMsg}`)
    resetFunc()
  })
}

export {
  stopCollectFunc,
  startOrDisableStatus,
  deleteResourceSite,
  setStartOrDisableRequestApi,
  setDeleteRequestApi,
  setStopCollectRequestApi
}
